home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8148 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.4 KB

  1. Path: goanna.cs.rmit.EDU.AU!not-for-mail
  2. From: ok@goanna.cs.rmit.EDU.AU (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: 15 Feb 1996 15:17:20 +1100
  6. Organization: Comp Sci, RMIT, Melbourne, Australia
  7. Message-ID: <4fuc4g$ovu@goanna.cs.rmit.EDU.AU>
  8. References: <4fms62$c0p@goanna.cs.rmit.EDU.AU> <824143624snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: goanna.cs.rmit.edu.au
  10. X-Newsreader: NN version 6.5.0 #0 (NOV)
  11.  
  12. Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  13. >Sure it is not difficult to implement sign-magnitude integer operations
  14. >but you would have to add extra CPU instructions in order to do so.
  15. >2's complement has the big advantage that it can be done using the same
  16. >instructions in many cases as unsigned arithmetic.
  17.  
  18. This argument turns out to be incorrect, and on two grounds.
  19. To start with, several modern RISC systems have two sets of arithmetic
  20. instructions anyway.  For example, the SPARC V8 Architecture Manual
  21. lists 
  22.     - duplicate integer loads (LDSB, LDSH, LDUB, LDUH)
  23.     - duplicate multiply/divide (UMUL, SMUL, UMULcc, SMULcc,
  24.       UDIV, SDIV, UDIVcc, SDIVcc)
  25. It already has 6 integer add and 6 integer subtract instructions; one more
  26. of each would hardly be a huge increase.  In addition to that, the Motorola
  27. 88000 has duplicate add and subtract instructions (5 signed adds, 5 unsigned,
  28. 5 signed subtracts, 5 unsigned).  These I looked up; from memory the R3000
  29. also has two sets of add and subtract instructions, but I can't look that up.
  30. The Intel 80960 has
  31.     - addo, subo, mulo, divo, remo, shlo, shro, cmpo (unsigned)
  32.     - addi, subi, mulo, divi, remi, shli, shri, cmpi (signed)
  33. which I just looked up.
  34.  
  35. Since having two sets of integer instructions is _already_ common, making
  36. one of them sign-and-magnitude would do _nothing_ to the opcode space.
  37.  
  38. But it gets better:  if you don't have to worry about C, you have no
  39. particular reason to _want_ unsigned arithmetic.  I used a B6700 for
  40. years, writing Fortran, COBOL, Pascal, Algol, Lisp, and SNOBOL, and
  41. never noticed that unsigned arithmetic wasn't there because there wasn't
  42. anything I wanted it for.  (Note that you can do all the bit twiddling
  43. you want on signed numbers, you don't need unsigned for that.)
  44.  
  45. >This is a non-issue. If you treat any expression resulting in 'the most
  46. >negative 2's complement integer' as an illegal result as far as your
  47. >code is concerned then you are back to the same situation as if you
  48. >were using sign-magnitude.
  49.  
  50. Sorry, this is a _real_ issue.  It's not _me_ that has to treat -2**(n-1)
  51. as an illegal result, it's the _hardware_, and it _doesn't_.  There's no
  52. compiler option for any of the compilers I use to make it do that.  Most
  53. of the compilers I use don't bother to check _any_ arithmetic results,
  54. and one SPARC Pascal compiler I used to use got some of the checks it
  55. did bother to do wrong.
  56.  
  57. >So as you can see the overall effect is the same in both cases.
  58.  
  59. The point is that 2s complement adders _don't_ report -2**(n-1) as overflow,
  60. don't have a bit in the program status word to make them do it, and so
  61. require extra instructions to check for wrong answers.  The shortest sequence
  62. I can think of on a SPARC to check for overflow is
  63.  
  64.     ADDcc    dest, src1, src2
  65.     TVS    <some trap number>
  66.     CMP    dest, <a register holding -(2**n-1)>
  67.     TE    <same trap number>
  68.  
  69. which is going to take 4 times as long as a plain add, or would if I could
  70. get a compiler to generate it.
  71.  
  72. >Do you check every signed integer operation for overflow (barring the ones
  73. >you can prove can't overflow)?
  74.  
  75. Damn straight I do, *if* the compiler will let me.
  76. I also believe in having the results checked even if I _have_ proved
  77. they can't overflow; I've made mistakes in proofs before.
  78.  
  79. I would remind readers that one reason some of the modern chips have
  80. signed as well as unsigned operations is that the signed operations
  81. DO raise an exception on overflow.
  82.  
  83. >Whether the answer is yes or no you are no worse off using 2's complement
  84. >that sign-magnitude (but for different reasons).
  85.  
  86. This conclusion makes no sense.  There is no reason to expect sign and
  87. magnitude arithmetic to slow down a computing *system* (though it could
  88. make a difference to the area of the ALU).  It simplifies the properties
  89. of arithmetic.  And it cannot be simulated without moderately high cost,
  90. and in any case that simulation is NOT available.
  91. -- 
  92. Election time; but how to get Labour _out_ without letting Liberal _in_?
  93. Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
  94.